package top.tangyh.lamp.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.mapper.BCarePgRecordMapper;
import top.tangyh.lamp.care.service.BCarePgRecordService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BCarePgRecordManager;
import top.tangyh.lamp.care.entity.BCarePgRecord;
import top.tangyh.lamp.care.vo.save.BCarePgRecordSaveVO;
import top.tangyh.lamp.care.vo.update.BCarePgRecordUpdateVO;
import top.tangyh.lamp.care.vo.result.BCarePgRecordResultVO;
import top.tangyh.lamp.care.vo.query.BCarePgRecordPageQuery;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 护理入科评估表
 * </p>
 *
 * @author zqb
 * @date 2024-07-26 10:31:53
 * @create [2024-07-26 10:31:53] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BCarePgRecordServiceImpl extends SuperServiceImpl<BCarePgRecordManager, Long, BCarePgRecord> implements BCarePgRecordService {

    private final BCarePgRecordMapper bCarePgRecordMapper;
    @Override
    public Integer countOfPgRecord(String hospitalNumber) {
        QueryWrapper<BCarePgRecord> queryWrapper=new QueryWrapper();
        List<Integer> typeList = new ArrayList<>();
        typeList.add(1);
        typeList.add(2);
        typeList.add(3);
        typeList.add(4);
        queryWrapper.in("type",typeList);
        queryWrapper.eq("hospital_number",hospitalNumber);
        queryWrapper.apply("date_format(pf_time,'%Y-%m-%d') <= DATE_FORMAT(now(),'%Y-%m-%d')");

        return bCarePgRecordMapper.selectCount(queryWrapper).intValue();
    }

    @Override
    public void checkPgByPfTime(BCarePgRecord po) {
        if(bCarePgRecordMapper.checkPgByPfTime(po.getPatientIdent(),po.getPfTime(),po.getPfr())){
            throw new BizException("此时间点已有该评分");
        }
    }

    @Override
    public List<BCarePgRecord> listCarePgRecordByPatientIdent(String patientIdent,Integer type,Long rkTime,Long ckTime) {
        QueryWrapper<BCarePgRecord> wrapper = new QueryWrapper<BCarePgRecord>();
        if(null != rkTime){
            wrapper.ge("pf_time", PDateUtils.getDateTimeOfTimestamp(rkTime));
        }
        if(null != ckTime){
            wrapper.le("pf_time",PDateUtils.getDateTimeOfTimestamp(ckTime));
        }
        wrapper.lambda().eq(BCarePgRecord::getPatientIdent, patientIdent)
                .eq(null != type , BCarePgRecord::getType,type);
        List<BCarePgRecord> result = this.list(wrapper);
        return result;
    }


}


